package com.xiake.app.web.resolver;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.xiake.core.model.ReturnDataModel;
import com.xiake.utils.JsonUtil;


@Component
public class MyExceptionHandler implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object obj, Exception e) {
		ReturnDataModel rdm = new ReturnDataModel(ReturnDataModel.RETURN_FAILURE, "网络异常，稍后再试吧!");
		PrintWriter out = null;
		try {
			response.setContentType("application/json");
			out = response.getWriter();
			String json = JsonUtil.objectToJson(rdm);
			out.print(json);
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally{
			if(out != null){
				out.flush();
				out.close();
			}
		}
		return null;
	}


}
